package com.slz3.business.module.messageManagement.mesUser.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.messageManagement.mesUser.domain.MesUser;
import com.slz3.business.module.messageManagement.mesUser.domain.MesUserFactory;
import com.slz3.business.module.messageManagement.mesUser.steward.MesUserSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MesUserServiceImpl implements MesUserService {

    @Override
    public MesUser save(MesUser mesUser) {
        return Repositorys.mesUser.save(MesUserFactory.initBaseFields(mesUser));
    }

    @Override
    public Page<MesUser> page(MultiValueMap<String, String> params) {
        Page<MesUser> mesUserPage = Repositorys.mesUser.findAll(
                MesUserSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return mesUserPage;
    }

    @Override
    public MesUser modify(MesUser mesUserNew) {
        return Repositorys.mesUser.saveAndFlush(MesUserFactory.initBaseFields(mesUserNew));
    }

    @Override
    public Optional<MesUser> single(UUID uuid) {
        return Repositorys.mesUser.findById(uuid);
    }

    @Override
    public List<MesUser> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.mesUser.findAllById(uuids);
    }

    @Override
    public List<MesUser> delete(List<MesUser> mesUserList) {
        mesUserList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MesUserFactory.initBaseFields(item);
        });
        return Services.mesUserTransactional.batchModify(mesUserList);
    }

}
